import echarts from 'echarts';

interface IConfig {
  dataX?: Array<string>,
  nameX?: string,
  nameY?: string,
  dataY?: Array<number>,
  data?: Array<any>,
  legend?: Array<any>,
  ref: any,
  title?: string,
  color?: Array<any>
}

export function SingleFigure(config: IConfig) {
  if(config.ref) {
    let myChart = echarts.init(config.ref);

    let option = {
      legend: {
        color: config.color,
        left: 'center',
        top: 'top'
    },
      tooltip : {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          label: {
            backgroundColor: '#6a7985'
          }
        }
      },
      toolbox: {
        feature: {
          saveAsImage: {}
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis : [
        {
          type : 'category',
          boundaryGap : false,
          data : config.dataX,
          axisLabel: {
            formatter: function(value){
              return value.split(" ").join("\n");
            }
          }
        }
      ],
      yAxis : [
        {
          type : 'value'
        }
      ],
      series : config.data
    };

    myChart.setOption(option)
  }
}
